<?php
/**
 * 合同模块
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9 0009
 * Time: 下午 2:32
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\ContractVali;
use App\Model\Backstage\Contract;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;

class ContractController extends Controller
{
    protected  $model;
    public function __construct()
    {
        $this->model = new Contract();
    }




    /** 合同列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function lists(Request $request)
    {
        $map= array();
        if ($request->input('number')){
            $map['number'] = trim($request->input('number'));
        }
        if ($request->input('customer_name')){   //客户姓名
            $map[] = ['customer_name', 'like', "%".trim($request->input('customer_name'))."%"];
        }
        if ($request->input('owner_name')){   //业主姓名
            $map[] = ['owner_name', 'like', "%".trim($request->input('owner_name'))."%"];

        }
        $res = $this->model->getList($map);
        if (!$res){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }

    /** 合同添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $parameter = $request->except('token');
        if(!ContractVali::validate($parameter)){
            return $this->error(Error::PARAM_MISSING);
        }
       $res = $this->model->add($parameter);
        if ($res === -1) {
            return $this->error(Error::CONTRACT_NUM_REPEAT);
        }
        if (!$res){
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('添加成功');
    }

    /** 合同修改
     * @param Request $request
     */
    public function edit(Request $request)
    {
        $parameter = $request->except('token');
        if(!ContractVali::validate($parameter) || !isset($parameter['id'])){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->model->edit($parameter);
        if (!$res){
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('修改成功');
    }
    /** 删除合同
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');
        if(!isset($id) || $id == ''){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->model->deletes($id);
        if($res === -1){
            return $this->error(Error::OPERATION_ERROR);
        }
        if (!$res){
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('操作成功');
    }

    //合同回收站列表
    public function RecycleList(Request $request,Contract $Contract){
        $param = $request->all();
        $where = [];
        if($request->input('start_time')){
            $where[] = ['updated_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $where[] = ['updated_at', '>=', strtotime(date('Y-m-d',strtotime('-3 month')).' 00:00:00')];
        }

        if($request->input('end_time')){
            $where[] = ['updated_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $where[] = ['updated_at', '<=', time()];
        }

        //关键词智能搜索
        $keywords = '';
        if ($request->input('keywords')){
            //
            $keywords = trim($request->input('keywords'));
        }

        $list = $Contract->recycleBinList(env('PAGE_LIMIT_ADMIN'),$where,$keywords);
        if (!$list) {
            $data['start'] = date('Y-m-d',strtotime('-3 month'));
            $data['end']  = date('Y-m-d',time());
            return $this->error(Error::NULL_DATA,$data);
        }
        $start = date('Y-m-d',strtotime('-3 month'));
        $end = date('Y-m-d',time());
        return $this->success('ok', $list,$start,$end);
    }

}